package org.go.threadpool;

import org.go.expcetion.GoException;

/**
 * <p>
 * Holds references to Scheduler instances - ensuring uniqueness, and
 * preventing garbage collection, and allowing 'global' lookups - all within a
 * ClassLoader space.
 * </p>
 * 
 * @author James House
 */
public class GoThreadPoolRepository {

	private static volatile GoThreadPoolRepository inst;

	public static synchronized GoThreadPoolRepository getInstance() {
		if (inst == null) {
			synchronized (GoThreadPoolRepository.class) {
				if (inst == null) {
					inst = new GoThreadPoolRepository();
				}
			}
		}
		return inst;
	}

	private GoThreadPool goThreadPool;

	//private HashMap<String, GoThreadPool> schedulers;

	private GoThreadPoolRepository() {
		//schedulers = new HashMap<String, GoThreadPool>();
	}

	public synchronized void bind(GoThreadPool threadPool) throws GoException {
		if (this.goThreadPool != null) {
			throw new GoException("ThreadPool with id '" + threadPool.getThreadPoolId() + "' already exists.");
		} else {
			this.goThreadPool = threadPool;
		}
		//		if (schedulers.get(goThreadPool.getThreadPoolId()) != null) {
		//			throw new GoException("ThreadPool with id '" + goThreadPool.getThreadPoolId() + "' already exists.");
		//		}
		//		schedulers.put(goThreadPool.getThreadPoolId(), goThreadPool);
	}

	public synchronized GoThreadPool lookup() {
		//return schedulers.get(poolId);
		return goThreadPool;
	}

	//	public synchronized Collection<GoThreadPool> lookupAll() {
	//		return java.util.Collections.unmodifiableCollection(schedulers.values());
	//	}

	//	public synchronized boolean remove(String schedName) {
	//		return (schedulers.remove(schedName) != null);
	//	}
}